# Contributor: Bartłomiej Piotrowski <bpiotrowski@alpinelinux.org>
# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer:
pkgname=lldb
# Note: Update together with llvm.
pkgver=19.1.2
pkgrel=0
_llvmver=${pkgver%%.*}
pkgdesc="Next generation, high-performance debugger"
arch="all"
url="https://llvm.org/"
license="Apache-2.0"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="
	clang$_llvmver-dev
	clang$_llvmver-static
	cmake
	doxygen
	gtest-dev
	libedit-dev
	libffi-dev
	libxml2-dev
	linux-headers
	llvm$_llvmver-dev
	llvm$_llvmver-gtest
	llvm$_llvmver-static
	ncurses-dev
	py3-sphinx
	python3-dev
	samurai
	swig
	xz
	"
subpackages="$pkgname-dev $pkgname-doc py3-$pkgname-pyc py3-$pkgname:py3"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/lldb-${pkgver//_/}.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/cmake-${pkgver//_/}.src.tar.xz
	llvm_slug.py
	atomic.patch
	fix-embedded_interpreter.patch
	"
builddir="$srcdir/$pkgname-${pkgver//_/}.src"
options="!check"  # FIXME: enable tests

prepare() {
	default_prepare
	mv "$srcdir"/cmake-${pkgver//_/}.src "$srcdir"/cmake

	# llvm_slug module copied from llvm-19.1.2.src/docs/llvm_slug.py
	# needed by docs/conf.py
	install -Dm0755 -t "$srcdir"/llvm/docs/ "$srcdir"/llvm_slug.py
}

build() {
	export CC=clang
	export CXX=clang++

	case "$CARCH" in
	arm*)
		# binutils bugs
		;;
	*)
		export CFLAGS="$CFLAGS -flto"
		export CXXFLAGS="$CXXFLAGS -flto"
		;;
	esac

	cmake -B build -G Ninja -Wno-dev \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_SKIP_RPATH=ON \
		-DLLDB_BUILT_STANDALONE=ON \
		-DLLDB_INCLUDE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DLLVM_ENABLE_SPHINX=ON \
		-DLLVM_LINK_LLVM_DYLIB=ON \
		-DCMAKE_POSITION_INDEPENDENT_CODE=ON

	cmake --build build --target all docs-lldb-man
}

check() {
	ninja -C build check-lldb
}

package() {
	# backtracing/debugging pretty much anything is going to need libc debug symbols
	# not everything, but the common case needs this, and it's a small dep in comparison
	depends="musl-dbg"

	DESTDIR="$pkgdir" cmake --install build
	install -Dm644 build/docs/man/lldb.1 -t "$pkgdir"/usr/share/man/man1
	python3 -m compileall -fq "$pkgdir"/usr/lib/python3*
}

dev() {
	default_dev

	# move back main symlink, because py3-lldb links to it
	mv "$subpkgdir"/usr/lib/liblldb.so "$pkgdir"/usr/lib/
}

py3() {
	pkgdesc="Python3 module for LLDB"
	depends="$pkgname=$pkgver-r$pkgrel python3"
	replaces="py-lldb"

	amove usr/lib/python3.*
}

sha512sums="
7c86c92bfc868debedc01354d8d0bc89693e5a98c572e90dc1b7e789343c4178b101e730dda98332f99907cb006ce3983c2195d6c3a949e0ac8f5028817f097a  lldb-19.1.2.src.tar.xz
9d13575a3e55a500dd5370591386a79ccc2164056448571620427f64236bf3fd1369090e3d5bdeef725b181d563462f331ff03c54e0667c01d079cf3af8b0c3f  cmake-19.1.2.src.tar.xz
5b65acf109e4b1e22411216d4b023f9975ea1fb04ec011fa71741fa7d13e3a0f1af2f9d4886782a73172f21c9debadf618fd814db04cfd98462a42b0f39e4c4a  llvm_slug.py
3d797904301c935f5ec255fc155e8425d56cd794fd4776e8965434021cc03d18e7a75299df47554b76ec94f722635999103ef58b3806b9bdba69acde02dceebd  atomic.patch
7b063349bc7084d5da118c72a5bea63aa3102e83e1cbbc5a7e5277205cf63805f9b5fcf9f02762397d7e3e7b34bcaa701f40738c90ba446c28168ddd53a7314b  fix-embedded_interpreter.patch
"
